﻿using Newtonsoft.Json;
using PosClient.Helper;
using PosClient.Interfaces;
using PosClient.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PosClient.Service
{
    public class PluService: IPluService
    {
        public void CachePlu(List<Plu> PluList)
        {
            if (PluList.Count > 0)
            {
                var conn = FreeClient.Redis;
                {
                    foreach(var plu in PluList.GroupBy(y => y.PluCode).Select(x => x.FirstOrDefault()).ToDictionary(k => k.PluCode))
                    {
                        conn.HSet(RedisKey.Redis_Plu_Main, plu.Key, JsonConvert.SerializeObject(plu.Value));
                    }
                }
            }
        }

        public void CacheBarcode(List<PluBarcode> BarcodeList)
        {
            if (BarcodeList.Count > 0)
            {
                using (var conn = FreeClient.Redis)
                {
                    foreach (var barcode in BarcodeList.GroupBy(y => y.Barcode).Select(x => x.FirstOrDefault()).ToDictionary(k => k.Barcode))
                    {
                        conn.HSet(RedisKey.Redis_Plu_Barcode, barcode.Key, JsonConvert.SerializeObject(barcode.Value));
                    }
                }
            }
        }

        public void CacheDiscount(List<PluDiscount> DiscountList)
        {
            if (DiscountList.Count > 0)
            {
                using (var conn = FreeClient.Redis)
                {
                    foreach (var discount in DiscountList.GroupBy(y => y.Barcode).Select(x => x.FirstOrDefault()).ToDictionary(k => k.Barcode))
                    {
                        conn.HSet(RedisKey.Redis_Plu_Main, discount.Key, JsonConvert.SerializeObject(discount.Value));
                    }
                }
            }
        }


        public Result<Plu> SearchPlu(string code)
        {
            Result<Plu> result = new Result<Plu>()
            {
                Success = true,
                Message = "请求成功"
            };
            var conn = FreeClient.Redis;
            {
                var resultBarcode = conn.HExists(RedisKey.Redis_Plu_Barcode, code);
                if (resultBarcode)
                {
                    var barcode = JsonConvert.DeserializeObject<PluBarcode>(conn.HGet(RedisKey.Redis_Plu_Barcode, code));
                    var resultPlu = conn.HExists(RedisKey.Redis_Plu_Main, barcode.PluCode);
                    if (resultPlu)
                    {
                        var plu = conn.HGet(RedisKey.Redis_Plu_Main, barcode.PluCode);
                        result.Data = JsonConvert.DeserializeObject<Plu>(plu);
                    }
                    else
                    {
                        result.Success = false;
                        result.Message = $"商品{code}不存在";
                    }
                }
                else
                {
                    var resultPlu = conn.HExists(RedisKey.Redis_Plu_Main, code);
                    if (resultPlu)
                    {
                        var plu = conn.HGet(RedisKey.Redis_Plu_Main, code);
                        result.Data = JsonConvert.DeserializeObject<Plu>(plu);
                    }
                    else
                    {
                        result.Success = false;
                        result.Message = $"商品{code}不存在";
                    }
                }
            }
            return result;
        }

    }
}
